using System.Web.Mvc;
using Uber.Core.Model;
using Uber.WebUI.Models;

namespace Uber.WebUI.Controllers
{
    public class RegisterController : Controller
    {
        public ActionResult Index()
        {
            return View(new AccountInput());
        }

        [HttpPost]
        [CaptchaValidator]
        public ActionResult Index(AccountInput input, bool captchaValid)
        {
            if (!ModelState.IsValid) return View(input);

            if (input.ConfirmPassword != input.Password) ModelState.AddModelError("ConfirmPassword", "Confirmation password doesn't match the password");
            if (!captchaValid) ModelState.AddModelError("captcha", "Captcha not valid");

            if (!ModelState.IsValid) return View(input);

            try
            {
                if (Fa.Account.Create(input.Email, input.Password, input.DisplayName))
                {
                    return RedirectToAction("Created", "Success", new {input.Email});
                }
            }catch(UberEx ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            return View();
        }
    }
}
